package br.ufpe.cin.vspeed.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author <a href="mailto:andreza.lba@gmail.com">Andreza Leite de Alencar</a>
 */
public final class FileUtils {

	private FileUtils() {
	}

	public static byte[] getBytesFromFile(final String file) throws IOException {
		return SerializationUtils.getBytesFromStream(SerializationUtils.createStream(file));
	}

	public static void writeFile(final byte[] bytes, final String file) throws IOException {
		final FileOutputStream output = new FileOutputStream(file);
		SerializationUtils.copy(new ByteArrayInputStream(bytes), output);
	}

	public static void writeFile(final byte[] bytes, final File file) throws IOException {
		final FileOutputStream output = new FileOutputStream(file);
		SerializationUtils.copy(new ByteArrayInputStream(bytes), output);
	}

	public static void copy(final String from, final String to) throws IOException {
		final FileOutputStream output = new FileOutputStream(to);
		SerializationUtils.copy(SerializationUtils.createStream(from), output);
		output.close();
	}

	public static void saveFile(final byte[] data, final String fileName) throws IOException {
		final File file = new File(fileName);
		if (file.exists()) {
			file.delete();
		}

		final OutputStream out = new FileOutputStream(fileName);
		out.write(data);
		out.close();
	}

	public static String getAbsolutePath(final String relativeLocation) {
		String absolutePath = null;
		final File file = new File(relativeLocation);

		if (file.exists()) {
			absolutePath = file.getAbsolutePath();
		}

		return absolutePath;
	}
}
